package com.teach.basic.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.common.bean.PageVo;
import com.common.bean.QueryCondition;
import com.teach.basic.dao.OrderDetailDao;
import com.teach.basic.dao.OrdersDao;
import com.teach.basic.model.dto.OrderDTO;
import com.teach.basic.model.po.OrderDetails;
import com.teach.basic.model.po.Orders;
import com.teach.util.PageUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;

/**
 * <p>
 *  订单表 服务实现类
 * </p>
 * @version 1.0
 * @author hzz
 * @since 2020-03-10
 */
@Service
public class OrdersServiceImpl extends ServiceImpl<OrdersDao, Orders> {
    @Autowired
    private OrdersDao orderDao;
    @Autowired
    private OrderDetailDao orderDetailDao;
    @Value("${spring.redis.state}")
    private String redisState;

    private String pubishchannel="order";
    @Transactional(rollbackFor = Exception.class)
    public int add(OrderDTO order){
        String ordersId=orderDao.getCode("orders");
        for (OrderDetails od : order.getDetails()){
            od.setOrdersId(ordersId);
            orderDetailDao.insert(od);
        }
        Orders orders=new Orders();
        orders.setId(ordersId);
        orders.setAmount(BigDecimal.valueOf(Double.valueOf(order.getAmount())));
        orders.setOperatorId(order.getOperatorId());
        orders.setType(order.getType());
        return this.baseMapper.insert(orders);
    }

    public PageVo queryPage(QueryCondition params,QueryWrapper<Orders> ordersQueryWrapper){
        IPage<Orders> page = this.page(
            new PageUtil<Orders>().getPage(params),ordersQueryWrapper);

        return new PageVo(page.getRecords(),page.getTotal(),(int)page.getSize(),(int)page.getCurrent());
    }

}
